package Client.Manager
{
	import Client.VO.ArmyVO;
	import Client.VO.UnitVO;
	
	/**
	 * class		ArmyMgr
	 * @desc		used to manage the armies
	 * @author		Aqua
	 * @date		2011-9-8
	 */
	public class ArmyMgr
	{
		//---------------------//
		//      Singleton      //
		//---------------------//
		
		/**************************************************************************
		 * function				instance getter
		 * @desc				a private function used to enforce singlton
		 * @return	ArmyMgr		singleton instance of this class
		 *************************************************************************/
		public static function get instance():ArmyMgr
		{
			if(null == _instance)
			{
				_instance = new ArmyMgr(singleton);
			}
		
			return _instance;
		}
		
		/**************************************************************************
		 * function			singleton
		 * @desc			a private function used to enforce singlton
		 *************************************************************************/
		private static function singleton():void
		{
			
		}
		
		
		//---------------------//
		//     Constructor     //
		//---------------------//
		
		/**************************************************************************
		 * function			ArmyMgr
		 * @desc			constructor of class
		 * @param	s		a private function used to enforce singlton
		 *************************************************************************/
		public function ArmyMgr(s:Function = null):void
		{
			if (singleton != s)
			{
				throw new Error("This is a singleton class!");
			}
		}
		
		
		//-----------------//
		//     Publics     //
		//-----------------//
		
		/**************************************************************************
		 * function			initialize
		 * @desc			initialize the manager, mainly load units' data
		 *************************************************************************/
		public function initialize():void
		{
			var xml		:XML;
			var xmlList	:XMLList;
			var item	:XML;
			var vo		:UnitVO;
			
			xml			= ResourceMgr.instance.getResource(ResourceMgr.XML_UNIT) as XML;
			xmlList		= xml.unit;
			_unitList	= {};
			
			for each(item in xmlList)
			{
				vo					= new UnitVO();
				vo.id				= item.id;
				vo.nameEn			= item.name_en;
				vo.nameCh			= item.name_ch;
				vo.power			= item.power;
				vo.musterPoint		= item.muster_point;
				vo.activeInSea		= item.active_in_sea;
				_unitList[vo.id]	= vo;
			}
		}
		
		/**************************************************************************
		 * function				setArmyPower
		 * @desc				set an army's power
		 * @param	armyVO		ArmyVO that contains the army's data
		 *************************************************************************/
		public function setArmyPower(armyVO:ArmyVO):void
		{
			var id		:String;
			var item	:UnitVO;
			
			id = armyVO.id;
			
			for each(item in _unitList)
			{
				if (item.id == id)
				{
					armyVO.power = item.power * armyVO.amount;
					break;
				}
			}
			
			// some special conditions need to be added
			// such as siege engine and some special event
		}
		
		
		//------------------//
		//     Privates     //
		//------------------//
		
		
		private static var _instance:ArmyMgr;
		
		private var _unitList	:Object;
	}
	
}